/**
 * 
 */
package GUI.GameBoard;


import java.awt.Dimension;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import Utils.Utils;

/**
 * @author Avia Tam
 * @author Assaf Miron
 *
 */
@SuppressWarnings("serial")
public class HousePanel extends JPanel {
	private Integer num_houses=0;
	private String imagePath = "/monopoly_house.png";
	private ImageIcon image;
	
    public HousePanel() {
    	//sets the placements to start from left to right
        BoxLayout boxLayout = new BoxLayout(this, BoxLayout.X_AXIS);
        //set layout
        this.setLayout(boxLayout);
        // Save the Image
        image = Utils.getImageIcon(imagePath);
        //set background to be transparent
        this.setOpaque(false);
        // Set the Preferred Size of the Panel to be as the Image Size
        setPreferredSize(new Dimension(50, 35));
        this.addComponentListener(new ResizeListener());
        
    }
 

    /**
     * Function receives number of houses from square and repaints them
     * @param numOfHouses The Number of Houses to Build
     */
    public void addHouses(int numOfHouses) {
        this.num_houses = numOfHouses;
        this.removeAll();
        for (int i=0 ; i < num_houses ; i++) {
            this.add (new JLabel(image, JLabel.CENTER));
        }
        // Validate and Repaint
        this.validate();
        this.repaint();
    }
    
    //just for test
    public static void main(String[] args) {
    	HousePanel a=new HousePanel();
    	a.addHouses(3);
        JFrame frame = new JFrame();
        frame.getContentPane().setBackground(null);
        frame.getContentPane().add(a);
        frame.setSize(60, 60);
        frame.setVisible(true);
    }
    
    /**
     * Clears all the Houses from the Panel 
     */
    public void clearHouses(){
        this.num_houses = 0;
        this.removeAll();
        // Validate and Repaint
        this.validate();
        this.repaint();
    }

    /**
	 * @author Avia Tam
	 * @author Assaf Miron
	 *
	 * Resize listener to ensure image is sized correctly for the panel
	 */
	class ResizeListener extends ComponentAdapter {

		/**
		 * Overrides ComponentAdapter.componentResized
		 * Ensures the image is correctly resized
		 * @param evt the event
		 */
		public void componentResized(ComponentEvent evt) {
			// Notify that the Component Resized
			super.componentResized(evt);
			// Check if this Component is Our Panel
			if(evt.getComponent() instanceof JPanel) {
				// Clear all the Components from the Picture Panel
				removeAll();
				// Create a new Resized Image	
				image = Utils.createScaledIcon(imagePath, getWidth(), getHeight(), getInsets());
				// Add the Resized Image to the Picture Panel
				addHouses(num_houses);
			}
		}
	}
    
}
